package cn.swust.util;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

public class UtilCommon {
	public static String getSerial(Date date, int index) {  
        long msel = date.getTime();  
        SimpleDateFormat fm = new SimpleDateFormat("MMddyyyyHHmmssSS");  
        msel += index;  
        date.setTime(msel);  
        String serials = fm.format(date);  
        return serials;  
    }  
    //检查文件格式
    public static boolean checkIsImage(String imgStr) {  
        boolean flag = false;  
        if (imgStr != null) {  
            if (imgStr.equalsIgnoreCase(".gif")  
                    || imgStr.equalsIgnoreCase(".jpg")  
                    || imgStr.equalsIgnoreCase(".jpeg")  
                    || imgStr.equalsIgnoreCase(".png")) {  
                flag = true;  
            }  
        }  
        return flag;  
    }
    //读取属性文件的指定key的值
    public static String getPropertiesByName(String name)
    {
    	Properties p = new Properties();
		try {
			p.load(DBUtil.class	.getResourceAsStream("/jdbc.properties"));
			
		} catch (IOException e2) {
			System.out.println(DBUtil.class+"找不到数据库配置属性文件");
			e2.printStackTrace();
		}
		return p.getProperty(name);
    }
    /**
     * SHA加密
     * @param plainText
     * @return
     */
    public static String md5s(String plainText)
	{
		 String str="";
		 try{
			 MessageDigest md = MessageDigest.getInstance("SHA");
			 md.update(plainText.getBytes());
			 byte b[] = md.digest();
			 int i;
			 StringBuffer buf = new StringBuffer("");
			 for (int offset = 0; offset < b.length; offset++) 
			 {
			    i = b[offset];
			    if (i < 0)
			    	i += 256;
			    if (i < 16)
			    	buf.append("0");
			    buf.append(Integer.toHexString(i));
			 }
			 str = buf.toString().toUpperCase();
			 return str;
		 } catch (NoSuchAlgorithmException e) {
			   e.printStackTrace();
			   return str.toUpperCase();
		 }
	}
    public static void main(String[] args) {
		System.out.println(md5s("0"));
	}
}
